package com.zs.backend.common.task;

import com.zs.backend.common.utils.CollectionUtil;
import com.zs.backend.modules.enterpriseOrder.model.EnterpriseOrder;
import com.zs.backend.modules.enterpriseOrder.service.EnterpriseOrderService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class OrderExpireTask {


    @Autowired
    private EnterpriseOrderService enterpriseOrderService;

    // 每隔一段时间执行一次 1S
    //@Scheduled(fixedRate = 1000)
    // 每天凌晨0点1分执行
    //@Scheduled(cron = "0 1 0 * * *")
    public void doTask() {
        System.out.println("定时任务执行中...");
        List<EnterpriseOrder> list =  enterpriseOrderService.getOrderNoExpires();
        if(CollectionUtil.isNullOrEmpty(list)){
            return;
        }
        Date currentDate = new Date();
        List<EnterpriseOrder> uptOrders = new ArrayList<>();
        for(EnterpriseOrder enterpriseOrder : list){
            if(currentDate.compareTo(enterpriseOrder.getContractEndTime()) > 0){
                EnterpriseOrder uptOrder = new EnterpriseOrder();
                uptOrder.setId(enterpriseOrder.getId());
                uptOrder.setExpire(true);
            }
        }
        if(CollectionUtil.isNotNullAndEmpty(uptOrders)){
            enterpriseOrderService.updateBatchById(uptOrders);
        }
    }

}
